<template>
  <div id="admin-manage" class="page-wrapper">
    <h1 class="main-title">创建设备</h1>
    <div class="selector-wrapper main-card wrapper">
      <div class="main-card">
        <el-form class="form" :inline="true" ref="form" :model="selectorData">
          <GroupSelector :selectorData="selectorData" />
        </el-form>
      </div>
      <el-button
        style="text-align: right;"
        type="primary"
        :disabled="selectorData.floorId === null"
        @click="onSubmit"
        >获取数据</el-button
      >
    </div>

    <!-- 表格 -->
    <h1 class="main-title">已有设备列表</h1>
    <div class="wrapper main-card">
      <div>
        <el-table :data="pageData" style="width: 100%">
          <el-table-column prop="master_id" label="总控编号"> </el-table-column>
          <el-table-column prop="region" label="楼栋"> </el-table-column>
          <el-table-column prop="layer" label="楼层"> </el-table-column>
          <el-table-column label="电器状态">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.state"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0"
                @change="handleSwitchChange(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="layer" label="模板名称"> </el-table-column>
          <el-table-column
            prop="meterRead"
            label="水表读数(吨)"
          ></el-table-column>
          <el-table-column
            prop="lastRead"
            label="上次水表读数(度)"
          ></el-table-column>
          <el-table-column prop="data" label="用表日期"></el-table-column>
          <el-table-column property="button" label="操作" width="180px">
            <template slot-scope="scope">
              <el-button>抄表</el-button>
              <el-button @click="modify(scope.row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页器 -->
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <!-- 表格 -->
  </div>
</template>

<script>
import GroupSelector from './components/all-device.vue'
import { addmasters, getmasters } from '@/api/meter'
export default {
  name: 'RoomInfo',
  components: {
    GroupSelector
  },
  data() {
    return {
      currentPage: 1,
      pageData: [],
      total: '',
      pageSize: 10,
      selectorData: {
        name: '001',
        buildingId: null,
        floorId: null
      },

      tableData: []
    }
  },
  mounted() {
    // this.fetchData()
  },
  methods: {
    // 分页逻辑
    queryByPage() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = this.currentPage * this.pageSize
      return this.tableData.slice(start, end)
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.pageData = this.queryByPage()
    },
    handleCurrentChange(val) {
      console.log(val)
      this.currentPage = val
      this.pageData = this.queryByPage()
    },
    // 获取服务器数据
    fetchData() {
      getmasters().then(res => {
        this.tableData = res.data.master
        this.total = res.data.master.length // 设置总数据量
        this.pageData = this.queryByPage() // 初始化分页数据
      })
    },
    onSubmit() {
      addmasters(this.selectorData).then(res => {
        this.fetchData() // 重新获取数据以反映创建的设备
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.wrapper {
  margin: 40px 0;
}

.selector-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
  /* 垂直居中 */
}

.list-wrapper {
  margin-top: 40px;
}

.el-form-item--small.el-form-item {
  margin-bottom: 0px;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 20px;
  /* Adjust margin as needed */
}
</style>
